let name = document.querySelector(".add-name");
let addressContent = document.querySelector(".address-table")
let province = document.querySelector(".add-province");
let city = document.querySelector(".add-city");
let district = document.querySelector(".add-district");
let streetname = document.querySelector(".add-streetname");
let phone = document.querySelector(".add-tel");
let baoc = document.querySelector(".baoc");


(function(){
	//渲染方法
	function Data(msg,obj,data = {}){
		json.get(URL + '/api_country', data, even =>{
			// console.log(even);
			if(even.code != 0){
				console.log(even);
				return;
			};	
			
			let str = `	<option>${msg}</option>`;
			(even.data.area || even.data).forEach(item => {
				str += `<option value = ${item.name}>${item.name}</option>`;
				
			});
			obj.innerHTML = str;
			
		});
	};
	
	
	Data("所在省份",province);
	
	province.onchange = function(){
	
		Data("所在城市",city,{province : this.value});
	}
	city.onchange = function(){

		Data("所在区",district,{province : province.value, city : this.value});
	}
	
	
})();
let isEdit = '';
//点击保存
baoc.onclick = function(){
	console.log(name.value, streetname,phone.value)
	if(name.value == "" || streetname.value == "" || phone.value ==""){
		alert('姓名 地址 手机号不能为空');
		return;
	}
	// if(name.value == "" || )
	//验证
	if(isEdit){
		// 先删除，再保存
		json.post(URL + '/api_address', {
			status : 'deleteAddress',
			userId : TOKEN,
			addressId : isEdit
		}, even => {
			// console.log(res);
			if(even.code != 0){
				console.log(even);
				return;
			};
			//删除成功，再保存			
			createAddress();
		})
	}else{
		//调用新增方法
		 createAddress();
	}
	
	function createAddress(){
		json.post(URL + '/api_address', {
			status : 'addAddress',	
			userId : TOKEN,
			province : province.value,
			city : city.value,
			district : district.value,
			streetname : streetname.value,
			takename : name.value,
			tel: phone.value
		}, even =>{
			console.log(even);
			if(even.code != 0){
				console.log(even);
				return;
			};
			 addressList()
			name.value = '';
			phone.value = '';
			streetname.value = '';
		}); 
	};
};
//渲染地址
function addressList(){
	json.post(URL + '/api_address', {
			status : 'getAddress',
			userId : TOKEN
		}, even =>{
			console.log(even);
			
			if(even.code != 0){
				console.log(even);
				return;
			};
			
			if(even.data.length  == 0){
				addressContent.innerHTML = '<h3>暂无地址，快去添加新地址吧</h3>';
				return;
			}
			
			addressContent.innerHTML = `
					<tr>
						<td>收货人</td>
						<td>所在地区</td>
						<td>街道地址</td>
						<td>电话/手机</td>
						<td>状态</td>
						<td>操作</td>
					</tr>
			`;
			even.data.forEach(item =>{
				let tr = document.createElement('tr');
			    tr.innerHTML = `
				<tr class="addres-list">
					<td>${item.takename}</td>
					<td>${item.province} ${item.city} ${item.district}</td>
					<td>${item.streetname}</td>
					<td>${item.tel}</td>
					<td class ="default-btn" address-id="${item.address_id}">${item.isDefault ? '默认地址' : '设置为默认'}</td>
					<td><span class="edit-btn" address-id="${item.address_id}">编辑</span>/
					<span class ="del-btn" address-id="${item.address_id}">删除</span></td>
				</tr>`
				
				addressContent.appendChild(tr);
			});
			
			addressedit(even.data)
		});
};

 addressList()
 
 //删除 编辑 设为默认
 
 function addressedit(data){
	 
	 addressContent.onclick = function(e){
		//  console.log(e.target.className);
		// console.log(e.target.parentNode.parentNode)
		 	if(e.target.className == 'default-btn' && e.target.innerHTML == '设置为默认'){
				json.post(URL + `/api_address`,{
					status : 'defaultAddress',
					userId : TOKEN,
					addressId : e.target.getAttribute('address-id')
				},even =>{
					if(even.code != 0){
						console.log(even);
						return;
					};
					//调用地址渲染
					addressList();
				})
			};
			//删除地址
			if(e.target.className == 'del-btn'){
				//请求
				json.post(URL + '/api_address', {
					status : 'deleteAddress',
					userId : TOKEN,
					addressId : e.target.getAttribute('address-id')
				}, even => {
					// console.log(res);
					if(even.code != 0){
						console.log(even);
						return;
					};
					//操作DOM
				
					e.target.parentNode.parentNode.remove();
				})
			};
			
			//编辑
			if(e.target.className == 'edit-btn'){
				let nowAddressId = e.target.getAttribute('address-id');
				//拿到当前地址的内容，设置到下面，还要发信号，这是编辑过来的			
				let [itemAddress] = data.filter(item => {
					return item.address_id == nowAddressId
				});
				//设置到下面
				name.value = itemAddress.takename;
				streetname.value = itemAddress.streetname;
				phone.value = itemAddress.tel;
				//设置信号，这是编辑过来的
				isEdit = itemAddress.address_id;
				
				console.log(isEdit)
			};
				
		 
	 }
 }